﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

[CustomEditor(typeof(GameConfig))]
public class GameConfigEditor : Editor {

    List<string> m_lstUsers = new List<string>();
    Dictionary<string, bool> m_dictState = new Dictionary<string, bool>();
    LogGroupData m_logGroupData;
    static string m_strDataPath = "Assets/Resources/LogConfig.asset";

    void OnEnable()
    {
        try
        {
            GameConfig script = target as GameConfig;
            LoadData();
            foreach (string log in script.LogGroups)
            {
                m_lstUsers.Add(log);
                m_dictState.Add(log, m_logGroupData.LogUsers.Contains(log));
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.Message);
        }
    }

    void LoadData()
    {
        if (File.Exists(m_strDataPath))
        {
            m_logGroupData = AssetDatabase.LoadAssetAtPath(m_strDataPath, typeof(LogGroupData)) as LogGroupData;
        }
        else
        {
            m_logGroupData = ScriptableObject.CreateInstance<LogGroupData>();
            AssetDatabase.CreateAsset(m_logGroupData, m_strDataPath);
        }
    }

    void OnDisable()
    {
        m_lstUsers.Clear();
        m_dictState.Clear();
        m_logGroupData = null;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        GUILayout.Space(10);
        GUILayout.Label("------LagGroup------");
        List<string> popArray = new List<string>();
        popArray.Add("None");
        string[] arrays = System.Enum.GetNames(typeof(Utility.Log.LogLevel));
        popArray.AddRange(arrays);

        m_logGroupData.logLevel = EditorGUILayout.Popup("Log开启等级", m_logGroupData.logLevel, popArray.ToArray());
        if (m_logGroupData.logLevel == 0)
        {
            m_logGroupData.logLevel = 1;
        }
        for (int i = 0; i < m_lstUsers.Count; i++)
        {
            m_dictState[m_lstUsers[i]] = EditorGUILayout.Toggle(m_lstUsers[i], m_dictState[m_lstUsers[i]]);
        }

        if (GUILayout.Button("Save"))
        {
            if (File.Exists(m_strDataPath))
            {
                m_logGroupData = AssetDatabase.LoadAssetAtPath(m_strDataPath, typeof(LogGroupData)) as LogGroupData;
                if (m_logGroupData != null)
                {
                    List<string> LogUsers = new List<string>();
                    foreach (var item in m_dictState.Keys)
                    {
                        if (m_dictState[item])
                            LogUsers.Add(item);
                    }
                    m_logGroupData.LogUsers = LogUsers;
                }
                EditorUtility.SetDirty(m_logGroupData);

                GameConfig script = target as GameConfig;
                script.SetLogUser();
            }
        }
        GUILayout.Label("---------LogGroup-----------");
        GUILayout.Space(10);
    }
}
